<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      :data="list"
      :props="{label:'name'}"
      check-strictly
      default-expand-all
      show-checkbox
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('success')">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { transDataToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permisson'
import { getRoleDetail, assignPrem } from '@/api/setting'
export default {
  name: 'VueHr2Assignpermission',
  props: {
    culId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadAssign()
  },
  methods: {
    async submit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPrem({ id: this.culId, permIds: permIds })
      this.$emit('success')
      console.log(res)
    },
    // 获取所有权限
    async  loadAssign() {
      try {
        const res = await getPermissionList()
        console.log(res)
        const detail = await getRoleDetail(this.culId)
        console.log(detail)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = transDataToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
